<?php 
$page=1;
if(isset($_REQUEST['page'])){
	$page=$_REQUEST['page'];
}
$rowpage=10;
$pagestart=($page-1)*$rowpage;
$pageend=$pagestart+$rowpage;

if(isset($_REQUEST['type'])){
$link='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$_SESSION['linkorder']=$link;	
}else{
$link='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'?type=0';
$_SESSION['linkorder']=$link;
}

$numpage=ceil(count($list_deal)/$rowpage);

$date = date("Y-m-d");
$date = strtotime(date("Y-m-d", strtotime($date)) . "-12 months");
if(isset($_REQUEST['m_start']))
$m_start = $_REQUEST['m_start'];
else
$m_start = date("Y-m-d",$date);
if(isset($_REQUEST['m_end']))
$m_end = $_REQUEST['m_end'];
else
$m_end=date("Y-m-d");
?>
<link rel="stylesheet" type="text/css" href="css/jsDatePick_ltr.min.css" />
<script type="text/javascript" src="js/jsDatePick.min.1.3.js"></script>

<script type="text/javascript">
window.onload = function(){
	new JsDatePick({
        useMode:2,
        target:"m_start",
        dateFormat:"%Y-%m-%d"
    });
	new JsDatePick({
        useMode:2,
        target:"m_end",
        dateFormat:"%Y-%m-%d"
    });
};
function getFormattedDate(year,month,day) {
	  month = month.length > 1 ? month : '0' + month;
	  day = day.length > 1 ? day : '0' + day;
	  return year + '-' + month + '-' + day;
}
function onchangetime(co){
	if(co==1){
		var d= new Date();
		document.getElementById('m_start').value=getFormattedDate(d.getFullYear(),(d.getMonth()+1).toString(),d.getDate().toString());
		document.getElementById('m_end').value=getFormattedDate(d.getFullYear(),(d.getMonth()+1).toString(),d.getDate().toString());
	}
	if(co==7){
		var d= new Date();
		document.getElementById('m_end').value= getFormattedDate(d.getFullYear(),(d.getMonth()+1).toString(),d.getDate().toString());
		d.setDate(d.getDate() -7);
		document.getElementById('m_start').value=getFormattedDate(d.getFullYear(),(d.getMonth()+1).toString(),d.getDate().toString());
		
	}
	if(co==15){
		var d= new Date();
		document.getElementById('m_end').value= getFormattedDate(d.getFullYear(),(d.getMonth()+1).toString(),d.getDate().toString());
		d.setDate(d.getDate() -15);
		document.getElementById('m_start').value=getFormattedDate(d.getFullYear(),(d.getMonth()+1).toString(),d.getDate().toString());
	}
	if(co==30){
		var d= new Date();
	/*d.setDate(d.getDate() - 1);*/
		document.getElementById('m_end').value= getFormattedDate(d.getFullYear(),(d.getMonth()+1).toString(),d.getDate().toString());
		d.setMonth(d.getMonth() - 1);
		document.getElementById('m_start').value=getFormattedDate(d.getFullYear(),(d.getMonth()+1).toString(),d.getDate().toString());
		
	}
	if(co==-30){
		var d= new Date();
		document.getElementById('m_end').value= getFormattedDate(d.getFullYear(),(d.getMonth()+1).toString(),d.getDate().toString());
		//d.setMonth(d.getMonth() - 1);
		document.getElementById('m_start').value=getFormattedDate(d.getFullYear(),(d.getMonth()+1).toString(),"01");
		
	}
}
function submit() {
	var ff = document.mform;
	ff.submit();
}

function goPage(link,on) {
	window.location.assign(link+"&page="+on)
}

function checkModify(oid,status){
	var hoi=confirm("<?=$l_areyousure?>");
    if(hoi==true){
		location.href="page/modify_order.php?oid="+oid+"&status="+status;
    }
}

function checkCanceled(oid) {
	var hoi=confirm("<?=$l_areyousure?>");
    if(hoi==true){
		location.href="page/cancel_order.php?oid="+oid;
    }
}

function checkRestore(oid) {
	var hoi=confirm("<?=$l_areyousure?>");
    if(hoi==true){
		location.href="page/restore_order.php?oid="+oid;
    }
}
</script>


<div class="content_table">
			<table cellspacing="0" cellpadding="0" border="0" style="border-collapse: collapse;margin-left: -10px;margin-top: -20px;width: 980px;background: url('images/title_03.gif') repeat-x scroll 0% 0% transparent;" >
				<thead>
					<tr>
						<th style="text-align: left;background: url('images/title_03.gif') repeat-x scroll 0% 0% transparent;height: 24px;">
						<?php 
						
						?>
						
						</th>
						
					</tr>
				</thead>
				</table>
				<br>
			<div class="search_box">
			<form name="mform" method="GET" action="">
			<input name="status" type="hidden" value="<?=$type?>">
				<div class="search_sbox">
					<ul>
						<li style="margin-top: 10px">Ngày tạo</li>
						<li><input name="m_start" id="m_start" type="text" value="<?=$m_start?>" readonly="readonly" style="width: 100px;">
						~<input name="m_end" id="m_end" type="text" value="<?=$m_end?>" readonly="readonly" style="width: 100px;"></li>
						
						<li class="divBtn" onclick="onchangetime(1)">Hôm nay</li>
						<li class="divBtn" onclick="onchangetime(-30)">Tháng này</li>
						<li class="divBtn" onclick="onchangetime(7)">Một tuần</li>
						<li class="divBtn" onclick="onchangetime(15)">Hai tuần</li>
						<li class="divBtn" onclick="onchangetime(30)">Một tháng</li>
					</ul>
					<ul>
						<li style="margin-top: 10px">Mã đơn hàng</li>
						<li><input name="m_code_order" type="text" value="<?=$m_code_order?>"></li>
						<li style="margin-top: 10px">Tên khách hàng</li>
						<li><input name="m_search" type="text" value="<?=$m_search?>"></li>
						<li><div class="divBtn" style="margin-right:15px" onclick="submit();">Tìm</div></li>
					</ul>
				</div>
			</form>
			</div>
			                
           
			
			
			 <table cellspacing="0" cellpadding="0" border="0"  style="border-collapse: collapse;width: 100%">				
				<thead>
					<tr>
						<th style="width: 25px;">STT</th>
						<th style="width: 40px;">Mã đơn hàng</th>
						<th style="width: 90px;">Khách hàng</th>
						<th style="width: 150px;">Deal</th>
						<th style="width: 30px;">Số lương</th>
						<th style="width: 50px;">Giá tiền</th>
						<th style="width: 50px;">Tổng tiền</th>
						<th style="width: 50px;"></th>
					</tr>
				</thead>
				<tbody>
				<?php
				$idtemp=-1;
				$no=0;
					foreach ($list_order as $value){
						
					?>
					<tr>
						<?php if($idtemp!=$value['id']){
							$no++;
							?>
						<td rowspan="<?=$value['sl']?>"><?=$no?></td>
						<td rowspan="<?=$value['sl']?>"><a href="<?=base_url('order/order_detail?oid='.$value['id'])?>"><?=$value['code_order']?></a></td>
						<td rowspan="<?=$value['sl']?>"><a href="<?=base_url('order/order_detail?oid='.$value['id'])?>"><?=$value['name']?></a></td>
						<?php }?>
						
						<td><a href="../deal_detail?did=<?=$value['id_deal']?>" target="_blank"><?=$value['deal']?> </a></td>
						<td><?=$value['quantity']?></td>
						<td><?=number_format($value['price'])?></td>
						
						<?php if($idtemp!=$value['id']){?>
						<td rowspan="<?=$value['sl']?>"><?=number_format($value['total'])?></td>
						<td rowspan="<?=$value['sl']?>">
						
							<?php if($value['status']==0){?>
								<div class="divBtn" style="width:60px; float:right; margin:0px 5px;" onclick="var hoi=confirm('Bạn có chắc không?');if(hoi==true){location.href='<?=base_url('order/changtype?oid='.$value['id'].'&type=1')?>'}">Xác nhận</div>
								<div class="divBtn" style="width:60px; float:right; margin:0px 5px;" onclick="var hoi=confirm('Bạn có chắc không?');if(hoi==true){location.href='<?=base_url('order/changtype?oid='.$value['id'].'&type=-1')?>'}">Hủy Order</div>
							<?php }?>
							<?php if($value['status']==1){?>
								<div class="divBtn" style="width:60px; float:right; margin:0px 5px;" onclick="var hoi=confirm('Bạn có chắc không?');if(hoi==true){location.href='<?=base_url('order/changtype?oid='.$value['id'].'&type=2')?>'}">Vận chuyển</div>
								<div class="divBtn" style="width:60px; float:right; margin:0px 5px;" onclick="var hoi=confirm('Bạn có chắc không?');if(hoi==true){location.href='<?=base_url('order/changtype?oid='.$value['id'].'&type=-1')?>'}">Hủy Order</div>
							<?php }?>
							<?php if($value['status']==2){?>
								<div class="divBtn" style="width:60px; float:right; margin:0px 5px;" onclick="var hoi=confirm('Bạn có chắc không?');if(hoi==true){location.href='<?=base_url('order/changtype?oid='.$value['id'].'&type=3')?>'}">Thanh toán</div>
								<div class="divBtn" style="width:60px; float:right; margin:0px 5px;" onclick="var hoi=confirm('Bạn có chắc không?');if(hoi==true){location.href='<?=base_url('order/changtype?oid='.$value['id'].'&type=-1')?>'}">Hủy Order</div>
							<?php }?>
							
							<?php if($value['status']==3){?>
								Đã thanh toán
							<?php }?>
							
							<?php if($value['status']==-1){?>
								Đã hủy
								<div class="divBtn" style="width:60px; float:right; margin:0px 5px;" onclick="var hoi=confirm('Bạn có chắc không?');if(hoi==true){location.href='<?=base_url('order/changtype?oid='.$value['id'].'&type=0')?>'}">Khôi Phục</div>
							<?php }?>
							
						</td>
						<?php $idtemp=$value['id'];
						}
						?>
					</tr>
				<?php }?>
				</tbody>
				
				
			</table>


			<div style="text-align:center; margin:10px 0px;">
				<div class="paginate_simple">
				<?php for($i=1;$i<=$numpage;$i++){
							if($page==$i){
						?>
						<strong><?=$i?></strong>
						<?php }else{?>
						<a href="javascript:goPage('<?=$link?>',<?=$i?>)"><?=$i?></a>
						<?php }?>
					<?php }?>
				</div>
			</div>

			</div>